package com.heyun.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heyun.bean.HeyunDept;
import com.heyun.bean.HeyunEmp;
import com.heyun.bean.HeyunSalesPerf;
import com.heyun.bean.Message;
import com.heyun.service.impl.SalesPerfServiceImpl;


@Controller
@RequestMapping("/SalesPerf")
public class SalesPerfController {
	@Autowired
	private SalesPerfServiceImpl spsi;
	//1 查询所有的员工姓名,在添加模态框中展示
	@RequestMapping("/getEmpName")
	@ResponseBody
	public Message getEmpName(){
		
		List<HeyunEmp> emplist=spsi.getEmpName();
		
		return Message.success().add("emplist", emplist);
		
	}
	//2 根据选择的人员的部门id,查找部门名称,展示在添加的模态框中
	@RequestMapping("/getDeptNameById")
	@ResponseBody
	public Message getDeptNameById(int deptId){
	
		HeyunDept dept=spsi.getDeptNameById(deptId);
		return Message.success().add("dept", dept);
		
	}
	
	//3 添加销售绩效
	@RequestMapping("/saveSalesPerf")
	@ResponseBody
	public Message saveSalesPerf(HeyunSalesPerf hsp,HttpSession session){
		 int empId=0;
         if(hsp.getName()!=null){
        	 empId=Integer.parseInt(hsp.getName());
         }
	     //由于取到的员工是员工,需要调用方法取到员工姓名
		HeyunEmp he=spsi.getEmpRealNameById(empId);
		System.out.println("要添加的员工真实姓名是===="+he.getEmpRealName());
		 // 获取预测生成时间
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String finalOperTime = sdf.format(System.currentTimeMillis());
		//  获取登陆的用户名,作为销售预测的操作人
		HeyunEmp emp=(HeyunEmp) session.getAttribute("user");
		String operator=emp.getEmpRealName();
		
		hsp.setFinalOperTime(finalOperTime);
		hsp.setOperator(operator);
		hsp.setName(he.getEmpRealName());
		spsi.saveSalesPerf(hsp);
		return Message.success();
		
	}
	//4 
	@RequestMapping("/findSalesPerf")
	@ResponseBody
	public Message findSalesPerf(int currentPage){
		
		Map<String, Object> map = spsi.findSalesPerf(currentPage);
		return Message.success().add("map", map);
		
	}
	//根据绩效的id查找出绩效信息,并展示在修改的页面上
	@RequestMapping(value = "getSalesPerfById/{salesPerfId}", method = RequestMethod.GET)
	@ResponseBody
	public Message getSalesPerfById(@PathVariable("salesPerfId") Integer salesPerfId){ 
		HeyunSalesPerf  hsp=spsi.getSalesPerfById(salesPerfId);
		return Message.success().add("hsp", hsp);
		
	}
	
  //根据id修改绩效的数据
	@RequestMapping("/updateSalesPerfById")
	@ResponseBody
	public Message updateSalesPerfById(HeyunSalesPerf hsp) {
        
		 int empId=0;
         if(hsp.getName()!=null){
        	 empId=Integer.parseInt(hsp.getName());
         }
	     //由于取到的员工是员工,需要调用方法取到员工姓名
			HeyunEmp he=spsi.getEmpRealNameById(empId);
			 // 获取预测生成时间
		    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String finalOperTime = sdf.format(System.currentTimeMillis());
			hsp.setName(he.getEmpRealName());
			hsp.setFinalOperTime(finalOperTime);
		spsi.updateSalsPerfById(hsp);
		return Message.success();
		
	}
	 //根据id删除绩效的数据 (单个删除)
	@RequestMapping(value = "deleteSalesPerfById/{salesPerfId}", method = RequestMethod.DELETE)
	@ResponseBody
	public Message deleteSalesPerfById(@PathVariable("salesPerfId") Integer salesPerfId) {
		System.out.println("================进入删除页面================="+"要删除的绩效id是"+salesPerfId);
		spsi.deleteSalsPerfById(salesPerfId);
		return Message.success();
		
	}
	
	//批量删除绩效

	@RequestMapping(value = "/deleteBatchSalesPerf/{attr}", method = RequestMethod.DELETE)
	@ResponseBody
	public Message deleteBatchSalesPlan(@PathVariable("attr") String attr) {
		System.err.println("要删除的id是========" + attr);
		String[] ids_str= attr.split(",");
		List<Integer> ids = new ArrayList<Integer>();
		for (String string : ids_str) {
			ids.add(Integer.valueOf(string));
		}
		System.out.println("///////////"+ids);
		spsi.deleteBatchSalesPerf(ids);;

		return Message.success();
	}
	
	/*
	 * 根据员工姓名数据取绩效数据
	 */
	@RequestMapping("/findSalesPerfByName")
	@ResponseBody
	public Message findSalesPerfByName(int currentPage,String name) {
		System.out.println("查询页数"+currentPage);
		System.out.println("要查询的姓名是"+name);
		Map<String,Object> map = spsi.findSalesPerfByName(currentPage,name);
		return Message.success().add("map", map);
	}
}
